//
//  MZYMyViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYMyViewController.h"
#import "MZYHeader.h"
#import "MZYmyTableViewCell.h"
#import "MZYmyWebViewController.h"


@interface MZYMyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *mArr;
@property(nonatomic, retain)NSMutableDictionary *dic;
@property (nonatomic, retain)NSMutableArray *imageArr;
@property(nonatomic, retain)MBProgressHUD *HUD;

@property(nonatomic, assign)NSInteger page;


@end

@implementation MZYMyViewController

- (void)dealloc {
    [_tableView release];
    [_mArr release];
    [_dic release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"展示";
//    self.navigationController.navigationBar.translucent = YES;
    self.mArr = [NSMutableArray array];
    
    [self createView];
    [self createData];
}

- (void)createView {
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 45)];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 400;
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    // 建立小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.detailsLabelText = @"请稍后";
    self.HUD.dimBackground = YES;
    
    //下拉
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(head)];
//    //上啦
//    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(foot)];
    
//    self.page = 1;



}

- (void)createData {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=10" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr = responseObject;
        
        for (NSDictionary *tempDic in arr) {
            MZYmyModel *myModel = [[MZYmyModel alloc] init];
            [myModel setValuesForKeysWithDictionary:tempDic];
            [self.mArr addObject:myModel];
            [myModel release];
        }
        self.HUD.hidden = YES;
        [self.tableView reloadData];
        
        [self.tableView.header endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}

- (void)head {
    
    [self createData];
    
}
//
//- (void)foot {
//    self.page++;
////    NSLog(@"%ld", (long)self.page);
//    
//    NSString *curl = [NSString stringWithFormat:@"http://watch-cdn.idailywatch.com/api/list/video/zh-hans?page=%ld&ver=iphone&app_ver=10", (long)self.page];
//    // 解析CollectionView
//    AFHTTPRequestOperationManager *conmanager = [AFHTTPRequestOperationManager manager];
//    [conmanager GET:curl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSArray *arr = responseObject;
//        
//        for (NSMutableDictionary *tempDic in arr) {
//            MZYmyModel *myModel = [[MZYmyModel alloc] init];
//            [myModel setValuesForKeysWithDictionary:tempDic];
//            [self.mArr addObject:myModel];
//            [myModel release];
//        }
//        
//        if (self.mArr.count > 0) {
//            self.HUD.hidden = YES;
//            [self.tableView reloadData];
//        }
//        [self.mArr removeLastObject];
//        [self.tableView.footer endRefreshing];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];
//    
//}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuse = @"reuse";
    MZYmyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[MZYmyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    MZYmyModel *myModel =self.mArr [indexPath.row];
    
    [cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:myModel.cover_thumb_hd] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    NSArray *zArr = myModel.entry_imgs;
    if (zArr.count > 1) {
        cell.bigImageView.frame = CGRectMake(0, 0, WIDTH, self.tableView.rowHeight / 4 * 3 - 20);
    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:zArr[0]] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    [cell.centerImageView sd_setImageWithURL:[NSURL URLWithString:zArr[1]] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    [cell.rightImageView sd_setImageWithURL:[NSURL URLWithString:zArr[2]] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    }else{
        cell.bigImageView.frame = CGRectMake(0, 0, WIDTH, self.tableView.rowHeight);
        [cell.contentView bringSubviewToFront:cell.bigImageView];
    }
    return cell;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MZYmyWebViewController *myWeb = [[MZYmyWebViewController alloc] init];
    MZYmyModel *myModel = self.mArr[indexPath.row];
    myWeb.hidesBottomBarWhenPushed = YES;
    myWeb.link_wechat = myModel.link_wechat;
    [self.navigationController pushViewController:myWeb animated:YES];
    [myWeb release];
}







//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    return self.mArr.count;
//}
//
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    MZYmyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuser" forIndexPath:indexPath];
//    MZYmyModel *myModel = self.mArr[indexPath.row];
//    cell.titleLabel.text = myModel.title;
//    [cell.titleImageView sd_setImageWithURL:[NSURL URLWithString:myModel.cover_thumb] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
//    cell.timeLabel.text = myModel.source;
//    cell.introduceLabel.text = myModel.summary;
//    cell.layer.masksToBounds = YES;
//    return cell;

//}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
